package com.gx.smart.lib.http.api;

import com.gx.smart.lib.http.api.asynctask.CallBack;
import com.gx.smart.lib.http.api.asynctask.GrpcAsyncTask;
import com.gx.smart.lib.http.api.config.ApiConfig;
import com.gx.smart.lib_http.BuildConfig;
import com.gx.wisestone.constructionappgrpclib.grpc.constructionappuser.ConstructionAppConstructionAppLoginRequest;
import com.gx.wisestone.constructionappgrpclib.grpc.constructionappuser.ConstructionAppConstructionAppLoginResponse;
import com.gx.wisestone.constructionappgrpclib.grpc.constructionappuser.ConstructionAppConstructionAppUserProviderGrpc;
import com.orhanobut.logger.Logger;

import java.util.concurrent.TimeUnit;

import io.grpc.ManagedChannel;

/**
 * @author xiaosy
 * @create 2020/8/10
 * @Describe
 **/
public class ConstructionAppConstructionAppUserProviderService {
    private String host = BuildConfig.JOYLIFE_GRPC_SERIVER_URL;
    private String port = "46604";

    private ConstructionAppConstructionAppUserProviderService() {

    }

    public static ConstructionAppConstructionAppUserProviderService getInstance() {
        return SingleTonHolder.INSTANCE;
    }

    /**
     * 获取AuthStub  设置超时时间  8秒
     *
     * @param channel
     * @return
     */
    private ConstructionAppConstructionAppUserProviderGrpc.ConstructionAppConstructionAppUserProviderBlockingStub getStub(ManagedChannel channel) {
        return ConstructionAppConstructionAppUserProviderGrpc.newBlockingStub(channel)
                .withDeadlineAfter(ApiConfig.TIMEOUT_NETWORK, TimeUnit.SECONDS);

    }

    /**
     * @param callBack
     * @return string mobile = 2;//手机号码
     */
    public GrpcAsyncTask<String, Void, ConstructionAppConstructionAppLoginResponse> login(
            final String mobile,
            CallBack callBack) {
        return new GrpcAsyncTask<String, Void, ConstructionAppConstructionAppLoginResponse>(callBack) {
            @Override
            protected ConstructionAppConstructionAppLoginResponse doRequestData(ManagedChannel channel) {
                ConstructionAppConstructionAppLoginRequest message = ConstructionAppConstructionAppLoginRequest.newBuilder()
                        .setMobile(mobile)
                        .setUaaId(ApiConfig.HUISHI_USER_SPACE_FORMAL)
                        .build();
                Logger.d(message);
                ConstructionAppConstructionAppLoginResponse response = null;
                try {
                    response = getStub(channel).login(message);
                    Logger.d(response);
                } catch (Exception e) {
                    Logger.e(e, "login");
                }
                return response;
            }
        }.setHost(host).setPort(port).doExecute();
    }

    private static class SingleTonHolder {
        private static final ConstructionAppConstructionAppUserProviderService INSTANCE = new ConstructionAppConstructionAppUserProviderService();
    }
}
